package com.haier.nengyuan.myapplication;

import android.graphics.PointF;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * page-1-02能源中心-01收益详情-日page-1-02能源中心-01收益详情-日
 */
public class BarChartFragment01 extends BaseFragment {

    private ArrayList<PointF> mPointArrayList = new ArrayList<>();
    private TextView mTvDayEarningTotal;
    private FrameLayout frameLayout;
    private float mDayEarningTotal;
    private String mStrHour;
    private String mStrDay;
    private String mStrMonth;
    private double dayTal;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_barchart_01, container, false);

        frameLayout = (FrameLayout) view.findViewById(R.id.frame_layout);
        mTvDayEarningTotal = (TextView) view.findViewById(R.id.tv_day_earning_total);

        return view;
    }

    //
//    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
//    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
//    @Override
//    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
//        super.onViewCreated(view, savedInstanceState);
//        mStrHour = MyDateUtil.changeH(System.currentTimeMillis());
//        mStrDay = MyDateUtil.changeD(System.currentTimeMillis());
//        Integer day = Integer.valueOf(mStrDay);
//        mStrMonth = MyDateUtil.changeM(System.currentTimeMillis());
//        Log.e("---", "onViewCreated: " + mStrMonth + ":" + mStrDay + ":" + mStrHour + "day:" + day);
//
//        if (mStrMonth.equals("09")) {
//            int mIntHour1 = (int) (Integer.parseInt(mStrHour) / 2);
//            WindowManager wm = getActivity().getWindowManager();
//            int width = (wm.getDefaultDisplay().getWidth() - 120) / 13;
//            for (int i = 0; i <= mIntHour1; i++) {
//                dayTal += points4[i][1];
//                mPointArrayList.add(new PointF(points4[i][0], (float) (0.85 * (float) (Float.valueOf(Variable.nineDayEnergy[day - 1]) * (points4[i][1] / 31.97)))));
//                float y = mPointArrayList.get(i).y;
//                Log.e("---", "onViewCreated: " + y + "元");
//                mDayEarningTotal = mDayEarningTotal + y;
//                View view1 = new View(getActivity());
//                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width - 10, (int) (y * 110));//日累计发电量柱状图宽高
//                layoutParams.gravity = Gravity.BOTTOM;
//                if (i == 0) {
//                    layoutParams.setMarginStart((width) * i + 60);
//
//                } else {
//                    layoutParams.setMarginStart((width) * i + 60 + 3 * i);
//
//                }
//                view1.setLayoutParams(layoutParams);
//                view1.setBackgroundColor(0xffffffff);
//
//                frameLayout.addView(view1);
//            }
//            String text = (float) (Math.round(mDayEarningTotal * 100)) / 100f + "";
//            Log.e("---", "onViewCreated: texttexttexttexttext" + text);
//            Variable.dayShou = text;
//            mTvDayEarningTotal.setText(text);
//        }
//        if (mStrMonth.equals("10")) {
//            /*10月*/
//            int mIntHour1 = (int) (Integer.parseInt(mStrHour) / 2);
//            WindowManager wm = getActivity().getWindowManager();
//            int width = (wm.getDefaultDisplay().getWidth() - 120) / 13;
//            for (int i = 0; i <= mIntHour1; i++) {
//                dayTal += points4[i][1];
//                mPointArrayList.add(new PointF(points4[i][0], (float) (0.85 * (float) (Float.valueOf(Variable.tenDayEnergy[day - 1]) * (points4[i][1] / 31.97)))));
//                float y = mPointArrayList.get(i).y;
//                Log.e("---", "onViewCreated: " + y + "元");
//                mDayEarningTotal = mDayEarningTotal + y;
//                View view1 = new View(getActivity());
//                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width - 10, (int) (y * 110));//日累计发电量柱状图宽高
//                layoutParams.gravity = Gravity.BOTTOM;
//                if (i == 0) {
//                    layoutParams.setMarginStart((width) * i + 60);
//
//                } else {
//                    layoutParams.setMarginStart((width) * i + 60 + 3 * i);
//
//                }
//                view1.setLayoutParams(layoutParams);
//                view1.setBackgroundColor(0xffffffff);
//
//                frameLayout.addView(view1);
//            }
//            String text = (float) (Math.round(mDayEarningTotal * 100)) / 100f + "";
//            Log.e("---", "onViewCreated: texttexttexttexttext" + text);
//            Variable.dayShou = text;
//            mTvDayEarningTotal.setText(text);
//        }
//        if (mStrMonth.equals("11")) {
//            /*10月*/
//            int mIntHour1 = (int) (Integer.parseInt(mStrHour) / 2);
//            WindowManager wm = getActivity().getWindowManager();
//            int width = (wm.getDefaultDisplay().getWidth() - 120) / 13;
//            for (int i = 0; i <= mIntHour1; i++) {
//                dayTal += points4[i][1];
//                mPointArrayList.add(new PointF(points4[i][0], (float) (0.85 * (float) (Float.valueOf(Variable.eleEnergy[day - 1]) * (points4[i][1] / 31.97)))));
//                float y = mPointArrayList.get(i).y;
//                Log.e("---", "onViewCreated: " + y + "元");
//                mDayEarningTotal = mDayEarningTotal + y;
//                View view1 = new View(getActivity());
//                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width - 10, (int) (y * 110));//日累计发电量柱状图宽高
//                layoutParams.gravity = Gravity.BOTTOM;
//                if (i == 0) {
//                    layoutParams.setMarginStart((width) * i + 60);
//
//                } else {
//                    layoutParams.setMarginStart((width) * i + 60 + 3 * i);
//
//                }
//                view1.setLayoutParams(layoutParams);
//                view1.setBackgroundColor(0xffffffff);
//
//                frameLayout.addView(view1);
//            }
//            String text = (float) (Math.round(mDayEarningTotal * 100)) / 100f + "";
//            Log.e("---", "onViewCreated: texttexttexttexttext" + text);
//            Variable.dayShou = text;
//            mTvDayEarningTotal.setText(text);
//        }
//
//    }
    private float mDataTotal;
    private float[] dayEnergy;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onResume() {
        super.onResume();
        mDataTotal = 0;
        frameLayout.removeAllViews();

        long time = System.currentTimeMillis();
        int mMonth = Integer.parseInt(MyDateUtil.changeM(time));
        int mDay = Integer.parseInt(MyDateUtil.changeD(time));
        int mIntHour = Integer.parseInt(MyDateUtil.changeH(time)) / 2;
        if (mMonth == 9) {
            dayEnergy = VirtualData.nineDayEnergy;
        } else if (mMonth == 10) {
            dayEnergy = VirtualData.tenDayEnergy;
        } else if (mMonth == 11) {
            dayEnergy = VirtualData.eleEnergy;
        } else if (mMonth == 12) {
            dayEnergy = VirtualData.decemberEnergy;
        } else if (mMonth == 1) {
            dayEnergy = VirtualData.oneEnergy;
        }

        WindowManager wm = getActivity().getWindowManager();
//        int width = (wm.getDefaultDisplay().getWidth() - 160) / 13;
        int width = (wm.getDefaultDisplay().getWidth() - 120) / 13;
        for (int i = 0; i <= mIntHour; i++) {
            float y = (VirtualData.pointsDay[i][1] / VirtualData.mdataDay) * dayEnergy[mDay - 1];
            mDataTotal += y;

            View view1 = new View(getActivity());
//            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width - 20, (int) (y * 0.91 * 70));
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width - 10, (int) (y * 110));//日累计发电量柱状图宽高
            layoutParams.gravity = Gravity.BOTTOM;
//            layoutParams.setMarginStart((width) * i + 60 + 10);
            if (i == 0) {
                layoutParams.setMarginStart((width) * i + 60);

            } else {
                layoutParams.setMarginStart((width) * i + 60 + 3 * i);

            }
            view1.setLayoutParams(layoutParams);
            view1.setBackgroundColor(0xffffffff);

            frameLayout.addView(view1);
        }

        mTvDayEarningTotal.setText((Math.round(mDataTotal * 0.85 * 100f)) / 100f + "");
    }

    @Override
    public int setRootView() {
        return 0;
    }

    @Override
    public void initViews() {
    }
}
